//
//  ShenHeViewController.m
//  Iderun
//
//  Created by  on 12-6-16.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ShenHeViewController.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
#import "XmlReader.h"
#import <QuartzCore/QuartzCore.h>
#import "MasterViewController.h"
#import "AuditedViewController.h"
#import "CustomNavigationController.h"

@implementation ShenHeViewController
@synthesize itemDictory,flag,content;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
            self.edgesForExtendedLayout = UIRectEdgeNone;
        }
        self.view.backgroundColor=[UIColor  grayColor];
        UITextView *textView=[[UITextView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, 300)];
        //textView.backgroundColor=[UIColor  clearColor];
        textView.layer.cornerRadius = 8;//设置那个圆角的有多圆
        textView.layer.borderWidth = 1;//设置边框的宽度，当然可以不要
        textView.layer.borderColor = [[UIColor whiteColor] CGColor];//设置边框的颜色
        textView.layer.masksToBounds = YES;//设为NO去试试
        self.content=textView;
        [textView  release];
        [self.view addSubview:self.content];
        [self.content  becomeFirstResponder];
    }
    return self;
}

- (void)viewDidLoad{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    UIBarButtonItem *leftButton=[[UIBarButtonItem alloc] initWithCustomView:[self customButtom:100 withFrame:CGRectMake(10, 0, 49, 29) action:@selector(closeShenHe:) normalImage:@"back_button.png" lightedImage:@"back_button.png" title:@""]];
    leftButton.style = UIBarButtonItemStylePlain;
    self.navigationItem.leftBarButtonItem=leftButton;
    [leftButton release];

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:[self customButtom:101 withFrame:CGRectMake(10, 0, 43, 29) action:@selector(saveShenheToserve:) normalImage:@"big_save.png" lightedImage:@"big_save.png" title:@""]];
    rightButton.style = UIBarButtonItemStylePlain;
    self.navigationItem.rightBarButtonItem=rightButton;
    [rightButton release];
    
    
}
-(void)  initContent{
    if (self.flag==0) {
        self.content.text=@"同意";
        self.title=@"审批意见";
    }else{
        self.content.text=@"";
         self.title=@"驳回意见";
    }
}

-(IBAction) closeShenHe:(id)sender{
    AppDelegate *de=(AppDelegate*)[UIApplication sharedApplication].delegate;
    [de.tabbar  dismissModalViewControllerAnimated:NO];
}



-(IBAction) saveShenheToserve:(id)sender{
    AppDelegate *appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
    
    if (urlRequest == nil) {
        urlRequest = [[UrlRequest alloc] initWithDelegate:self requestSelector:@selector(parseReceivedData:) errorSelector:@selector(errorCallback)];
    }
    NSString *TASK_ID=[[self.itemDictory objectForKey:@"TASK_ID"] objectForKey:@"text"];
    NSString *TASK_STATUS=@"A";
    if(flag==1){
      TASK_STATUS=@"NA";
        NSString *contentStr=[self.content.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        if (nil==contentStr||[@"" isEqualToString:contentStr]) {
            UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"请输入驳回意见" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            alertView.tag=99;
            [alertView show];
            [alertView  release];
            return;
        }
    }
    NSString *TASK_RESULT=[self.content.text stringByAppendingString:@"(from iphone)"];
    NSMutableDictionary *param=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"1",@"TASK_FLAG",TASK_STATUS,@"TASK_STATUS",TASK_RESULT,@"TASK_RESULT",TASK_ID,@"TASK_ID", nil];
    [urlRequest saveShenheToserve:param];
    [MBProgressHUD showHUDAddedTo:appdelegate.tabbar.view animated:NO];

}

-(void)parseReceivedData:(id)data{
    AppDelegate *appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
     [MBProgressHUD  hideHUDForView:appdelegate.tabbar.view animated:NO];
    if (data==nil||[data isEqualToString:@""]) {
        NSLog(@"shenhe receivedString=%@",@"空");
       
        return;
    }
   // NSError **error;
    //NSDictionary *resultData=[XmlReader dictionaryForXMLString:data error:error];
   // NSDictionary *userPasswordDict=[[[[[[[resultData  objectForKey:@"soap:Envelope"] objectForKey:@"soap:Body"] objectForKey:@"GetDataSetSqlResponse"] objectForKey:@"GetDataSetSqlResult"] objectForKey:@"diffgr:diffgram"] objectForKey:@"NewDataSet"] objectForKey:@"Table"];
    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"审核成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
       alertView.tag=100;
        [alertView show];
        [alertView  release];
    
    
}

-(void)errorCallback{
    AppDelegate *appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
    [MBProgressHUD  hideHUDForView:appdelegate.tabbar.view animated:NO];
    NSString* okTitle = @"提醒";
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:@"网路异常" delegate:nil cancelButtonTitle:okTitle otherButtonTitles:nil];
    alert.tag=88;
    [alert show];
    [alert release];
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (alertView.tag==100) {
        AppDelegate *appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
        [appdelegate.tabbar  dismissModalViewControllerAnimated:NO]; 
        CustomNavigationController *navigationController_master=(CustomNavigationController*)[[appdelegate.tabbar viewControllers] objectAtIndex:0];
        [navigationController_master popToRootViewControllerAnimated:YES];
        /*MasterViewController *masterViewController=(MasterViewController*)[navigationController_master.viewControllers objectAtIndex:0];
         [masterViewController loadData];
         
         UINavigationController *navigationController_audited=[[appdelegate.tabbar viewControllers] objectAtIndex:1];
         AuditedViewController *auditedViewController=(AuditedViewController*)[navigationController_audited.viewControllers objectAtIndex:0];
         [auditedViewController loadData];*/
    }
   
    
    
}


- (UIButton*) customButtom:(NSUInteger)numberOfTag withFrame:(CGRect)frame action:(SEL)action normalImage:(NSString*)normalImage lightedImage:(NSString*)lightedImage  title:(NSString *)title{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    button.tag=numberOfTag;
    button.backgroundColor=[UIColor clearColor];
    UIImage *normal_Image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:normalImage ofType:nil]];
    UIImage *lighted_Image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:lightedImage ofType:nil]];
    [button setTitle:title forState:UIControlStateNormal];
    [button setImage:normal_Image forState:UIControlStateNormal];
    [button setImage:lighted_Image forState:UIControlStateHighlighted];
    [button setImage:lighted_Image forState:UIControlStateSelected];
    button.adjustsImageWhenHighlighted = NO;
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation==UIDeviceOrientationPortrait);
}
- (BOOL)shouldAutorotate{
    return NO;
}
- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

-(void)  dealloc{
    [itemDictory  release];
    [content  release];
    [urlRequest release];
    [super dealloc];
}
@end
